import {useEffect, useState} from "react";
import {Button, Form, Modal, Row, message} from "antd";
import {ApiFindInfo} from "@/api";
import useCard from "@/hooks/useCard";
import useBoolean from "@/hooks/useBoolean";
import FormList from "@/components/common/form/formList";
import DndTable from "@/components/common/table/dndTable";
import {InputFormItem} from "@/components/common/form/formItem";
import {columnsIndex, columnsRelevanceId, columnsSetting, columnsTitle} from "@/components/Columns";

const TYPE = 2;

export default function IndexTabs() {
    const [form] = Form.useForm();
    const [visibly, on, off] = useBoolean(false);
    const [onDel, onCreateOrEdit] = useCard({});
    const [row, setRow] = useState<any>("");

    const [data, setData] = useState([]);

    const columns = [
        columnsIndex,
        columnsTitle,
        columnsRelevanceId,
        {
            ...columnsSetting,
            render: (_: any, row: any) => (
                <div>
                    <Button type="primary" size="small" onClick={() => handleEdit(row)}>
                        编辑
                    </Button>

                    <Button danger style={{marginLeft: 20}} onClick={() => handleDel(row)} size="small">
                        删除
                    </Button>
                </div>
            ),
        },
    ];

    const handleDel = (row: any) => {
        onDel([{pkId: row.pkId}], reqFindList);
    };

    const handleEdit = (row: any) => {
        setRow(row);
        on();
        form.setFieldsValue(row);
    };

    const handleOk = async () => {
        try {
            const val = await form.validateFields();
            let list: any[] = [];
            if (val.tags) {
                list = val.tags.map((item: any) => ({...item, type: TYPE}));
            } else {
                list = [{...val, type: TYPE, pkId: row.pkId}];
            }
            const res = await onCreateOrEdit(list);
            if (res) {
                reqFindList(onCancel());
            }
        } catch (error) {
            console.log(error);
        }
    };

    const onCancel = () => {
        setRow("");
        off();
        form.setFieldsValue("");
    };
    //查找列表
    const reqFindList = (callBack?: any) => {
        ApiFindInfo({type: TYPE})
            .then((res: any) => {
                setData(res.data);
                callBack && callBack();
            })
            .catch((err: any) => {
                message.error(err);
            });
    };

    useEffect(() => {
        reqFindList();
    }, []);

    return (
        <>
            <Row>
                <Button type="primary" onClick={on}>
                    添加Tabs
                </Button>
                &nbsp;&nbsp;
                <Button type="primary" onClick={() => onCreateOrEdit(data)}>
                    保存排序
                </Button>
            </Row>
            <DndTable style={{marginTop: 20}} setData={setData} data={data} columns={columns} />
            <Modal
                destroyOnClose
                width={700}
                title={`${row ? "编辑" : "添加"}Tabs`}
                open={visibly}
                onOk={handleOk}
                onCancel={onCancel}
            >
                <Form form={form} labelAlign="left" labelCol={{span: row ? 3 : 6}} preserve={false}>
                    {row ? (
                        <>
                            <InputFormItem label="标题" name="title" required />
                            <InputFormItem label="关联ID" name="rid" required type="number" />
                        </>
                    ) : (
                        <>
                            <FormList
                                rightLabel="关联ID"
                                leftName="title"
                                leftLabel="标题"
                                rightName="rid"
                                rightRequired={true}
                                leftRequired={true}
                            />
                        </>
                    )}
                </Form>
            </Modal>
        </>
    );
}
